package com.ai.service.made.impl;

import com.ai.entity.made.DelAlarmLog;
import com.ai.entity.made.DeleteSpecifiedRangeParam;
import com.ai.mapper.made.DelAlarmLogMapper;
import com.ai.service.made.IDelAlarmLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiaok
 * @since 2024-07-26
 */
@Service
public class DelAlarmLogServiceImpl extends ServiceImpl<DelAlarmLogMapper, DelAlarmLog> implements IDelAlarmLogService {

    @Override
    public void saveSuccDelLog(DeleteSpecifiedRangeParam deleteSpecifiedRangeParam, Integer i,String serverId) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        DelAlarmLog delAlarmLog = new DelAlarmLog();
        delAlarmLog.setBeginTime(LocalDate.parse(deleteSpecifiedRangeParam.getDatebegin(),formatter).atStartOfDay());
        delAlarmLog.setEndTime(LocalDate.parse(deleteSpecifiedRangeParam.getDateend(),formatter).atStartOfDay());
        delAlarmLog.setDeleteBatch(UUID.randomUUID().toString().replace("-",""));
        delAlarmLog.setDeleteStatus(String.valueOf(i));
        delAlarmLog.setCreateTime(LocalDateTime.now());
        delAlarmLog.setServerId(serverId);

        save(delAlarmLog);

    }

    @Override
    public void savepartAbnormalDelLog(DeleteSpecifiedRangeParam deleteSpecifiedRangeParam, int i, List<String> delFailList,String serverId) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        String deleteBatch = UUID.randomUUID().toString().replace("-", "");

        if (!CollectionUtils.isEmpty(delFailList)){
            for (String failDate : delFailList) {
                DelAlarmLog delAlarmLog = new DelAlarmLog();
                delAlarmLog.setBeginTime(LocalDate.parse(deleteSpecifiedRangeParam.getDatebegin(),formatter).atStartOfDay());
                delAlarmLog.setEndTime(LocalDate.parse(deleteSpecifiedRangeParam.getDateend(),formatter).atStartOfDay());
                delAlarmLog.setDeleteBatch(deleteBatch);
                delAlarmLog.setDeleteStatus(String.valueOf(i));
                delAlarmLog.setCreateTime(LocalDateTime.now());
                delAlarmLog.setAbnormalDate(failDate);
                delAlarmLog.setServerId(serverId);
                save(delAlarmLog);
            }
        }
    }

    @Override
    public void saveAllAbnormalDelLog(DeleteSpecifiedRangeParam deleteSpecifiedRangeParam, Integer i,String serverId) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        DelAlarmLog delAlarmLog = new DelAlarmLog();
        delAlarmLog.setBeginTime(LocalDate.parse(deleteSpecifiedRangeParam.getDatebegin(),formatter).atStartOfDay());
        delAlarmLog.setEndTime(LocalDate.parse(deleteSpecifiedRangeParam.getDateend(),formatter).atStartOfDay());
        delAlarmLog.setDeleteBatch(UUID.randomUUID().toString().replace("-", ""));
        delAlarmLog.setDeleteStatus(String.valueOf(i));
        delAlarmLog.setCreateTime(LocalDateTime.now());
        delAlarmLog.setServerId(serverId);
        save(delAlarmLog);
    }
}
